
class Base(object):

    @classmethod
    def from_dict(cls, attrs):
        clean_dict = cls.make_record(attrs)
        c = cls(**clean_dict)
        return c

    @classmethod
    def set_schema(cls, schema):
        cls.__table__.schema = schema

    @classmethod
    def params_to_dict(cls, params, mapping=None, filter=None, dict=None):
        ''' turn key/value array from database into a dictionary
            to specify filters, send filter=set(['time', 'etc...']) 
        '''
        retVal = {}
        if dict is not None:
            retVal = dict

        # put params into our dict
        for p in params:
            # filter variables based on name
            if filter and p.name in filter:
                continue

            # we can map keys in the return, ala parameter {time:at}, thus the return dict is {'at':'12:00 AM'}  
            name = p.name
            if mapping and name in mapping:
                name = mapping[name]
            retVal[name] = p.value

        return retVal


    def persist(self, ses):
        ''' Put self into the database... 
        '''
        ses.add(self)
        ses.commit()

    def to_dict(self):
        ''' convert a SA object into a dict that is serializable to JSON
        ''' 
        ret_val = self.__dict__

        ''' not crazy about this hack, but ... 
            the __dict__ on a SqlAlchemy object contains hidden crap that we delete from the class dict
        '''
        if set(['_sa_instance_state']).issubset(ret_val):
            del ret_val['_sa_instance_state']

        ''' we're using 'created' as the date parameter, so cnvert values to strings
            TODO: better would be to detect date & datetime objects, and convert those...
        '''
        if set(['created']).issubset(ret_val):
            ret_val['created'] = ret_val['created'].__str__();

        return ret_val 
